package com.qxzn.module.mine.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.widget.ToastUtil;
import com.qxzn.app.util.ActivityStack;
import com.qxzn.app.util.SharePreferenceUtil;
import com.qxzn.module.mine.R;
import com.qxzn.module.mine.R2;

import java.io.File;

import butterknife.BindView;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UpdateFaceResultActivity extends BaseActivity {

    @BindView(R2.id.btn_next)
    Button btn_next;
    @BindView(R2.id.circleImageView)
    ImageView circleImageView;
    @BindView(R2.id.tv_prompt)
    TextView prompt;
    private File file;
    private boolean ok;

    @Override
    protected int initLayout() {
        return R.layout.activity_update_face_result;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText("重置人脸识别");
    }

    private void done() {
        ActivityStack.finishAllWithoutActivity("com.qxzn.app.desktoprobot.MainActivity", "com.qxzn.module.mine.activity.UserInfoActivity");
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        file = (File) getIntent().getSerializableExtra("file");
        Glide.with(this).load(file).into(circleImageView);
        ok = getIntent().getBooleanExtra("ok", false);
        if (ok) {
            prompt.setText(R.string.face_detect_ok_prompt);
            btn_next.setText("上传人脸照片");
            btn_next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    btn_next.setVisibility(View.INVISIBLE);
                    MultipartBody.Part fileBody = HttpRequest.getPartFileBody(file);
                    RequestBody device_id = HttpRequest.getPartBody(SharePreferenceUtil.getDeviceId(UpdateFaceResultActivity.this));
                    RequestBody username = HttpRequest.getPartBody(SharePreferenceUtil.getUserName(UpdateFaceResultActivity.this));
                    HttpRequest.getService().modifyFace(device_id, username, null,  fileBody).enqueue(new Callback<BaseBean>() {
                        @Override
                        public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                            BaseBean baseBean = response.body();
                            if (baseBean.getCode() == 200) {
                                ToastUtil.showTextToast(UpdateFaceResultActivity.this, "人脸照片设置成功");
                                done();
                            } else {
                                ToastUtil.showTextToast(UpdateFaceResultActivity.this, baseBean.getMsg());
                            }
                        }

                        @Override
                        public void onFailure(Call<BaseBean> call, Throwable t) {
                            btn_next.setVisibility(View.VISIBLE);
                            ToastUtil.showTextToast(UpdateFaceResultActivity.this, getString(R.string.network_error));
                        }
                    });
                }
            });
        } else {
            prompt.setText(R.string.face_detect_error_prompt);
            btn_next.setText("返回");
            btn_next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
    }
}
